#include "window_heart.h"
#include "window_manager.h"

uint8_t  Heart_Num=123;   //定义心率数值

//修改默认字体  LV_FONT_DEFAULT


extern window_manger_t window_manger[];

lv_obj_t * heart_img=NULL;
lv_obj_t * heart_number=NULL;

static lv_timer_t * timer,*heart_timer;  //设置定时器

uint8_t zoom_num=0;
uint8_t Zoom_FLAG=1;
static void my_timer(lv_timer_t * timer)
{
	
lv_obj_t *	obj =lv_tileview_get_tile_act(lv_scr_act());
	
//	if(obj != window_manger[WINDOW_HEART].window_obj)
//	{
//	   lv_timer_del(heart_timer); 
//		 heart_timer=NULL;
//	 
//	  if(timer!= NULL)
//		{
//		  lv_timer_del(timer);
//			 timer=NULL;
//		}
//	return ;
//	}
//	
	
	
	lv_img_set_zoom(heart_img, zoom_num); //设置图片缩放
	if(Zoom_FLAG)
	{
	    zoom_num+=10;
		if(zoom_num>=250)
		{
		  Zoom_FLAG=0;
		}
	}else
	{
			 zoom_num-=10;
			if(zoom_num<=0)
			{
				Zoom_FLAG=1;
			}
	}
}

//显示当前的心率数值
static void heart_timer_handler(lv_timer_t * timer)
{

	Heart_Num=lv_rand(60,120);
	lv_label_set_text_fmt(heart_number, "#ffffff %3u#",Heart_Num);
}
static void my_event_cb(lv_event_t * event)
{
	
	printf("LV_EVENT PRESSED\r\n");
	timer      =lv_timer_create(my_timer, 50,NULL);
	heart_timer=lv_timer_create(heart_timer_handler, 1000,NULL);
	#if 0
	lv_dir_t  dir = lv_indev_get_gesture_dir(lv_indev_get_act());//获得手势的方向
	
	  lv_indev_t * indev = lv_indev_get_act(); //获取当前的坐标
    if(indev == NULL)  return;

    lv_point_t vect;
    lv_indev_get_vect(indev, &vect);
   lv_coord_t x =0;
   lv_coord_t y=0;
	
	
	
	if(LV_DIR_RIGHT  == dir)  
	{
		
		 x = lv_obj_get_x(task_obj) + vect.x;
	  if(x>0)
		{
		   x=0;
		}
    y = lv_obj_get_y(task_obj);// + vect.y;
    lv_obj_set_pos(task_obj, x, y);
		printf("LV_DIR_RIGHT\r\n");
	}else if(LV_DIR_LEFT  == dir)
	{
		
		x = lv_obj_get_x(task_obj) + vect.x;
	  if(x<-150)
		{
		   x=-150;
		}
	
    y = lv_obj_get_y(task_obj);
    lv_obj_set_pos(task_obj, x, y);
		
	  printf("LV_DIR_LEFT\r\n");
	}else if(LV_DIR_TOP  == dir)
	{
	
		
		x = lv_obj_get_x(menu_obj);
	  if(y<-360)
		{
		   y=-360;
		}
    y = lv_obj_get_y(menu_obj)+ vect.y;
    lv_obj_set_pos(menu_obj, x, y);
		
		
		  printf("LV_DIR_TOP\r\n"); 
		
		
	}else if(LV_DIR_BOTTOM  == dir)
	{
		
		x = lv_obj_get_x(menu_obj);
	  if(y>0)
		{
		   y=0;
		}
    y = lv_obj_get_y(menu_obj) + vect.y;
    lv_obj_set_pos(menu_obj, x, y);
		
		
		
	  printf("LV_DIR_BOTTOM\r\n");
	}
	
  #endif
	
}	


void window_heart_init()
{
	  printf("%s\r\n",__FUNCTION__);
	
	  heart_img = lv_img_create(window_manger[WINDOW_HEART].window_obj);
	  lv_img_set_src(heart_img, &hr_beats_2);
		lv_obj_set_pos(heart_img,240/2-37,60);
	
		lv_obj_t * heart_log = lv_img_create(window_manger[WINDOW_HEART].window_obj);
	  lv_img_set_src(heart_log, &hr_bg_log);
		lv_obj_set_pos(heart_log,240/2 - 120,210 - 40);
	
	
		lv_obj_t * heart_font = lv_img_create(window_manger[WINDOW_HEART].window_obj);
	  lv_img_set_src(heart_font, &hr_Bpm_ch);
	  //lv_img_set_src(heart_font, &hr_Bpm_en);
		lv_obj_set_pos(heart_font,240/2 +40 ,190 -40);
#if 1	
	timer      =lv_timer_create(my_timer, 50,NULL);
	heart_timer=lv_timer_create(heart_timer_handler, 1000,NULL);
#endif
	
	  lv_img_set_antialias(heart_img, true);


	  heart_number = lv_label_create(window_manger[WINDOW_HEART].window_obj);
    lv_label_set_recolor(heart_number, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_align(heart_number, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    //lv_label_set_text(heart_number, "#ffffff 123#");
		lv_label_set_text_fmt(heart_number, "#ffffff %3u#",Heart_Num);
	  lv_obj_set_pos(heart_number,240/2 - 40,180 -40);
		
		
		
	//显示心率字符
	lv_obj_t*  logo = lv_label_create(window_manger[WINDOW_HEART].window_obj); // 创建一个标签，用于显示滑块的滑动值
	lv_label_set_recolor(logo, true);                        /*Enable re-coloring by commands in the text*/
	lv_obj_set_style_text_font(logo, &language_36, 0);
	if(current_language==LANGUAGE_TYPE_CHINESE)
	{
		lv_label_set_text_fmt(logo,"#ffffff 心率#");
	}else 
	if(current_language==LANGUAGE_TYPE_ENGLISH)
	{
		lv_label_set_text_fmt(logo,"#ffffff Heart#");
	}
	lv_obj_align(logo, LV_ALIGN_TOP_MID,0,10);
		
}
